﻿/// <reference path="../_references.js" />

(function (e) {
    e.apiRepository = {
        post: function (resource, dat, callback) {
            $.ajax({
                url: "api/" + resource,
                cache: false,
                type: 'POST',
                data: JSON.stringify(dat),
                contentType: "application/json;charset=utf-8",
                dataType: "JSON",
                success: function (data) {
                    if (callback)
                        callback(null, data);
                },
                error: function (x, y, z) {
                    console.log(y);
                    console.log(z);
                    var message = JSON.parse(x.responseText).ExceptionMessage || JSON.parse(x.responseText).Message;
                    e.flash.error("<strong>Exception: </strong>" + message);
                    callback(message, null);
                }
            });
        },
        getAll: function (resource, callback) {
            $.ajax({
                url: "api/" + resource,
                cache: false,
                type: 'GET',
                contentType: "application/json;charset=utf-8",
                dataType: "JSON",
                success: function (data) {
                    if (callback)
                        callback(null, data);
                },
                error: function (x) {
                    var message = JSON.parse(x.responseText).ExceptionMessage || JSON.parse(x.responseText).Message;
                    e.flash.error("<strong>Exception: </strong>" + message);
                    callback(message, null);
                }
            });
        },
        getOne: function (resource, id, callback) {
            $.ajax({
                url: "api/" + resource + "/" + id,
                cache: false,
                type: 'GET',
                contentType: "application/json;charset=utf-8",
                dataType: "JSON",
                success: function (data) {
                    if (callback)
                        callback(null, data);
                },
                error: function (x) {
                    var message = JSON.parse(x.responseText).ExceptionMessage || JSON.parse(x.responseText).Message;
                    e.flash.error("<strong>Exception: </strong>" + message);
                    callback(message, null);
                }
            });
        },
        update: function (resource, dat, callback) {
            $.ajax({
                url: "api/" + resource + "/" + dat.Id,
                cache: false,
                type: 'PUT',
                data: JSON.stringify(dat),
                contentType: "application/json;charset=utf-8",
                dataType: "JSON",
                success: function () {
                    if (callback)
                        callback(null);
                },
                error: function (x) {
                    var message = JSON.parse(x.responseText).ExceptionMessage || JSON.parse(x.responseText).Message;
                    e.flash.error("<strong>Exception: </strong>" + message);
                    callback(message);
                }
            });
        },
        del: function (resource, id, callback) {
            $.ajax({
                url: "api/" + resource + "/" + id,
                type: 'DELETE',
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    if (callback)
                        callback(null, data);
                },
                error: function (x) {
                    var message = JSON.parse(x.responseText).ExceptionMessage || JSON.parse(x.responseText).Message;
                    e.flash.error("<strong>Exception: </strong>" + message);
                    callback(message, null);
                }
            });
        }
    };
} (this.eg = this.eg || {}));